import CryptoJS from "crypto-js";
import { getCookie } from ".";
// 获取 tooken
export function getToken() {
  let token = localStorage.getItem("token");
  if(getCookie("IPSSESSION") && token !== getCookie("IPSSESSION")) {
    localStorage.setItem("token", getCookie("IPSSESSION"));
    token = localStorage.getItem("token");
  }
  if (!token) {
    token = getCookie("IPSSESSION");
  }
  return token;
}
// 获取随机值
export function getRandomCode(length = 12) {
  if (length > 0) {
    var data = [
      "0",
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "A",
      "B",
      "C",
      "D",
      "E",
      "F",
      "G",
      "H",
      "I",
      "J",
      "K",
      "L",
      "M",
      "N",
      "O",
      "P",
      "Q",
      "R",
      "S",
      "T",
      "U",
      "V",
      "W",
      "X",
      "Y",
      "Z",
      "a",
      "b",
      "c",
      "d",
      "e",
      "f",
      "g",
      "h",
      "i",
      "j",
      "k",
      "l",
      "m",
      "n",
      "o",
      "p",
      "q",
      "r",
      "s",
      "t",
      "u",
      "v",
      "w",
      "x",
      "y",
      "z",
    ];
    var nums = "";
    for (var i = 0; i < length; i++) {
      var r = parseInt(''+Math.random() * 61);
      nums += data[r];
    }
    return nums;
  } else {
    return false;
  }
}
/**
 * 鉴权 规则
 * 有token时：sign=MD5(token+timestamp+nonce)
 * 无token时：sign=MD5(timestamp+nonce)
 */
export const getAuthentication = () => {
  const token = getToken(),
    timestamp = ''+parseInt('' + new Date().getTime() / 1000),
    nonce = getRandomCode() as string;
  let sign = CryptoJS.MD5(token+timestamp+nonce).toString();
  if(!token) {
    sign = CryptoJS.MD5(timestamp+nonce).toString();
  }
  return {
    token,
    timestamp,
    nonce,
    sign
  }
};

export function getProps(data?: string): {[key:number|string]:any}{
    const url = document.location.href,
        tempStr = url;
    let tempArr,
        tempKV;
    let props = {};
    tempArr = tempStr.split('?')
    tempArr = tempArr[1] ? tempArr[1] : ''
    tempArr = tempArr.split('&')
    if(data) {
        tempArr = data.split('?')
        tempArr = tempArr[1] ? tempArr[1] : ''
        tempArr = tempArr.split('&')
    }
    let tempKey,
        tempValue;
    for (let i = 0; i < tempArr.length; i++) {
        tempKV = tempArr[i].split('=');
        tempKey = tempKV[0];
        tempKV.splice(0, 1);
        tempValue = tempKV.join('=');
        Object.assign(props, {
            [tempKey]: tempValue
        })
    }
    return props;
}



const md5 = (str:string) => CryptoJS.MD5(str).toString();

/**
 * @description 获取签名
 */
export function getEncryptedParam(token: string, param?: {[key:number|string]:any}) {
  let nonce = getRandomCode();
  let timestamp = parseInt(''+new Date().getTime() / 1000);

  let newParam = `nonce=${nonce}&timestamp=${timestamp}&token=${token}`;

  // let arr = [nonce, timestamp, token];
  let arr = [token, timestamp, nonce];

  if (param) {
    for (var key in param) {
      arr.push(param[key]);
      newParam = `${newParam}&${key}=${param[key]}`;
    }
  }
  arr.sort();
  let str = "";
  for (let i = 0; i < arr.length; i++) {
    str = `${str}${arr[i]}`;
  }
  let sign = md5(str);
  newParam = `${newParam}&sign=${sign}`;
  let resObj = {
    nonce: nonce,
    timestamp: timestamp,
    sign: sign,
    token: token,
    param: newParam,
  };

  return resObj;
}
